package com.yaoxun.gateway.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import com.yaoxun.gateway.service.RouteDefinitionService;
import com.yaoxun.gateway.subscribe.RouterSubscribe;

@Configuration
@EnableConfigurationProperties(value = { GatewayProperties.class })
public class GatewaySubscribeConfiguration {

	@Bean
	public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(factory);
		RouterSubscribe routerSubscribe = routerSubscribe();
		container.addMessageListener(routerSubscribe, routerSubscribe.getTopic());
		return container;
	}
	
	@Bean
	public RouterSubscribe routerSubscribe() {
		return new RouterSubscribe();
	}
	
	@Bean
	public RouteDefinitionService routeDefinitionService() {
		return new RouteDefinitionService();
	}
	
}
